#import "DDButton.h"
CGFloat const BUTTONHEIGHT = 44;
@interface DDButton ()
@property (nonatomic, strong) DDImageView *IMG;
@property (nonatomic, strong) CCLabel *label;
@end
@implementation DDButton
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.layer.masksToBounds = YES;
        self.autoTitleSize = NO;
    }
    return self;
}
- (DDImageView *)IMG
{
    if (!_IMG)
    {
       _IMG = [[DDImageView alloc] init];
        [self addSubview:_IMG];
        _IMG.c_edges.c_equal(UIEdgeInsetsZero);
    }
    return _IMG;
}
- (CCLabel *)label
{
    if (!_label)
    {
        _label = [[CCLabel alloc] init];
        _label.textColor = [UIColor whiteColor];
        [self addSubview:_label];
        _label.c_center.c_equal(self);
    }
    return _label;
}
- (void)setImageName:(NSString *)imageName
{
    if (_imageName != imageName)
    {
        _imageName = imageName;
        self.IMG.imageName = imageName;
    }
}
- (void)setTitle:(NSString *)title
{
    if (_title != title)
    {
        _title = title;
        self.label.text = title;
        [self sizeToFit];
    }
}
- (void)setFont:(UIFont *)font
{
    if (_font != font)
    {
        _font = font;
        self.label.font = font;
        [self sizeToFit];
    }
}
- (void)setAutoTitleSize:(BOOL)autoTitleSize
{
    if (_autoTitleSize != autoTitleSize)
    {
        _autoTitleSize = autoTitleSize;
        [self sizeToFit];
    }
}
- (void)sizeToFit
{
    if (_autoTitleSize)
    {
        self.bounds = CGRectMake(0, 0, ceil(self.label.bounds.size.width) + 5, ceil(self.label.bounds.size.height) + 5);
        self.c_size.c_equal(self.bounds.size);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    self.alpha = 0.5;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    self.alpha = 1.0;
}
@end
